<?php


namespace app\admin\controller;


use app\admin\model\CarBindStore;
use app\BaseController;
use app\lib\MiniWechat;
use app\Request;

class Station extends  BaseController
{
    public function getList(Request  $request)
    {
        $page = $request->post('page', 1);
        $limit = $request->post('limit', 10);
        $result = \app\admin\model\Station::order('sort desc id desc')
            ->where('store_id',  $request->post('store_id'))
            ->page($page, $limit)
            ->select();
        foreach ($result as $item) {
            $item->qrcode_path = $request->domain() . $item->qrcode_path;
        }
        $total = \app\admin\model\Station::where('store_id',  $request->post('store_id'))->count();

        $data = [
            'total' => $total,
            'current' => $page,
            'perpage' => $limit,
            'data' =>  $result
        ];

        return show(config('status.success'), '操作成功', $data);
    }

    public function operateDoor(Request  $request)
    {
        $type = $request->post('type');
        $station = \app\admin\model\Station::find($request->post('id'));
        $user = new \app\api\controller\User(app());
        $result = $user->operateDoor($type, 0, $request->post('id'), $station->dev_id, 0, 3);
        if ($user->isSuccessByDoor) {
            return show(config('status.success'), '操作成功');
        } else {
            return $result;
        }
    }

    public function buildQrCode(Request  $request)
    {
        $id = $request->post('id');
        $station = \app\admin\model\Station::where('id', $id)->findOrEmpty();
        if ($station->isEmpty()) {
            return show(config('status.error'), '工位不存在');
        }
        $manager_id=0;
        $store_id = $station->store_id??0;
        if(!empty($store_id)){
            $carbindstore = \app\admin\model\Store::where(['id'=>$store_id])->find();
            if(!empty($carbindstore)){
                $manager_id = $carbindstore->manager_id??0;
            }
        }
        $wechat = new MiniWechat();
        $image = $wechat->createQrCode('s_' . $id."_{$store_id}_{$manager_id}", 'pages/index/UnlicensedCar/closeDoor');
        $fileName = '/images/qrcode_' . $id . '.png';
        $ret = file_put_contents(public_path() . $fileName, $image, true);
        $station->qrcode_path = $fileName;
        $station->save();
        return show(config('status.success'), '操作成功');
    }

    /**
     * 创建
     * @param Request $request
     * @return \think\response\Json
     */
    public function create(Request $request)
    {
        $name = $request->post('name');
        \app\admin\model\Station::create([
            'name'        => $name,
            'thumb_url'   => $request->post('thumb_url'),
            'dev_id'      => $request->post('dev_id'),
            'serialno'    => $request->post('serialno'),
            'description' => $request->post('description'),
            'sort'        => $request->post('sort'),
            'store_id'    => $request->post('store_id'),
            'manager_id' => $this->userInfo['id']??0,
        ]);
        return show(config('status.success'), '操作成功');
    }

    /**
     * 更新
     * @param Request $request
     * @return \think\response\Json
     */
    public function update(Request $request)
    {
        $name = $request->post('name');
        \app\admin\model\Station::where(['id' => $request->post('id')])->update([
            'name' => $name,
            'dev_id'      => $request->post('dev_id'),
            'serialno'    => $request->post('serialno'),
            'thumb_url'   => $request->post('thumb_url'),
            'description' => $request->post('description'),
            'sort' => $request->post('sort')
        ]);

        return show(config('status.success'), '操作成功');
    }

    public function delete(Request  $request)
    {
        \app\admin\model\Station::destroy($request->post('id'));
        return show(config('status.success'), '操作成功');
    }

    /**
     * 是否允许添加工位
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function isAllowAdd(Request  $request){
        $where['manager_id'] = $this->userInfo['id'];
        $manage = \app\admin\model\Manager::find($where);
        $num = \app\admin\model\Station::where($where)->count('id');
        if($manage['allow_station']==0 || $num>=$manage['allow_station']){
            return show(config('status.error'), "您允许添加的工位数为{$manage['allow_station']}家,您已添加{$num}家,如想添加请联系平台。");
        }else{
            return show(config('status.success'), '操作成功');
        }
    }
}